package com.huyucheng.todolist

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_todolist.*

class TodoListFragment : Fragment() {

    companion object {
        val TAG: String = "TodoListFragment"
    }

    lateinit var todoDao: TodoDao
    lateinit var todoListViewModel: TodoListViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (context == null)
            Log.i("TodoListFragment", "context is null")
        todoDao = AppDatabase.getDatabase(this.context?.applicationContext).todoDao()
        todoListViewModel = ViewModelProviders
            .of(this, TodoListViewModelFactory(todoDao))
            .get(TodoListViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_todolist, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val layoutManager = LinearLayoutManager(this.context)
        recyceler_todolist.layoutManager = layoutManager

        val checkedStatus = when (switch_checked_unchecked.selectedItemId ) {
            R.id.unchecked_items -> TodoListViewModel.UNCHECKED
            R.id.checked_items -> TodoListViewModel.CHECKED
            else -> 0
        }

        val adapter: TodoAdapter = TodoAdapter(todoListViewModel, todoDao, checkedStatus)
        recyceler_todolist.adapter = adapter
        fab.setOnClickListener {
            gotoAnotherFragment()
        }

        todoListViewModel.checkedTodoList.observe(this, Observer {
            adapter.notifyDataSetChanged()
            recyceler_todolist.postInvalidate()
        })

        todoListViewModel.uncheckedTodoList.observe(this, Observer {
            adapter.notifyDataSetChanged()
            recyceler_todolist.postInvalidate()
        })

        // bottom navigation view switch checked or unchecked todoList
        switch_checked_unchecked.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.unchecked_items -> {
                    adapter.checkedStatus = TodoListViewModel.UNCHECKED
                }
                R.id.checked_items -> {
                    adapter.checkedStatus = TodoListViewModel.CHECKED
                }
            }
            adapter.notifyDataSetChanged()
            recyceler_todolist.postInvalidate()
            true
        }
    }

    private fun gotoAnotherFragment() {
        val transaction: FragmentTransaction? = fragmentManager?.beginTransaction()
        val fragment = fragmentManager?.findFragmentByTag("AddTodoFragment")
        if (transaction == null) {
            Log.e(TAG, "gotoAnotherFragment: failed to begin fragment transaction!")
            return
        }
        if (fragment == null) {
            transaction.add(R.id.fragment_container, AddTodoFragment(), "AddTodoFragment")
        } else {
            transaction.show(fragment)
        }
        transaction.hide(this)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun reloadDataFromDB() {
        todoListViewModel.reload()
    }
}